package com.eascs.admin.o2o.loan.notify.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.o2o.backfee.entity.O2OComBackFee;
import com.eascs.admin.o2o.backfee.service.O2OComBankFeeService;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.paymentdetail.entity.O2oPaymentDetailEntity;
import com.eascs.paymentdetail.service.O2oPaymentDetailService;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 退保证金管理费流程
 */
@Service
public class BFMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(BFMsgNotifyServiceImpl.class);

	@Autowired
	private O2OComBankFeeService o2OComBankFeeService;
	
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;
	
	@Autowired
	private O2oPaymentDetailService o2oPaymentDetailService;
	
	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply){
		ModelResult<O2oLoanapplyEntity> modelResult =new ModelResult<>();
		log.info("启动退保证金管理费流程通知:[{}]", StatusContexts.WFTYPE_BACKFEE);
		loanapply = tfFlow(notifyParam,loanapply);
		modelResult.withModel(loanapply);
		return modelResult;
	}

	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add(ProcessTemplate.PROECESS_TYPE_REFUND_MGR_FEE);
		list.add(ProcessTemplate.PROECESS_TYPE_REFUND_MGR_FEE_FQ);
		return list;
	}

	
	@Override
	public Boolean checkReturn() {
		return Boolean.FALSE;
	}
	
	
	/**
	 * 
	 * 退费流程
	 * 
	 * @param notifyParam
	 * @return
	 */
	private O2oLoanapplyEntity tfFlow(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply) {
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {
			log.info("退费流程通过：ReferId:[{}]", notifyParam.getReferId());
			O2OComBackFee o2OComBackFee = o2OComBankFeeService.queryEntity(notifyParam.getReferId(), StatusContexts.BZJ_STATUS_RUN);
			// 2；修改 O2OComBackFee 中的状态
			o2OComBackFee.setBillStatus(StatusContexts.WFTYPE_STATUS_CLO);
			/*
			 * if (StatusContexts.DATA_SOURCE_OLD.equals(o2OComBackFee.
			 * getDataSource())) { // 如果是老系统则修改老系统中保证金管理费的值 OaBackFeeEntity
			 * oaBackFeeEntity =
			 * o2OComBankFeeService.findOaBackFee(o2OComBackFee.getBillCode());
			 * LoanapplyExpand loanapplyExpand =
			 * OaBackFeeTransO2Oloanapply.transloanapplyExpand(oaBackFeeEntity);
			 * loanapplyExpand = insertPaymentDetail(loanapplyExpand,
			 * o2OComBackFee); // 插入明细表,处理剩余的要修改库的数据 int updateresult =
			 * o2OComBankFeeService.updateBackFee(loanapplyExpand); if
			 * (updateresult > 0) { log.info("老系统ReferId:[{}],更新老系统主表数据成功",
			 * notifyParam.getReferId()); } else {
			 * log.info("老系统ReferId:[{}],更新老系统主表数据失败",
			 * notifyParam.getReferId()); } int i =
			 * o2OComBankFeeService.updateById(o2OComBackFee); if (i > 0) {
			 * log.info("老系统ReferId:[{}],更新退费表状态成功", notifyParam.getReferId());
			 * } else { log.info("老系统ReferId:[{}],更新退费表状态失败",
			 * notifyParam.getReferId()); } } else {
			 */
			LoanapplyExpand loanapplyExpand = new LoanapplyExpand();
			// 1：更新loanapplyExpand中已退未退金额
			loanapplyExpand = loanapplyExpandService.queryLoanapplyNo(notifyParam.getReferId());
			// 已退金额 = 以前已退金额 + 已退(本次退费金额)
			loanapplyExpand = insertPaymentDetail(loanapplyExpand, o2OComBackFee);//// 插入明细表
			if (loanapplyExpand.getRedManger().compareTo(loanapplyExpand.getRetaManage()) <= 0 && loanapplyExpand.getRedRisk().compareTo(loanapplyExpand.getRetaRisk()) <= 0 && loanapplyExpand.getRedDeposite().compareTo(loanapplyExpand.getRetaDeposite()) <= 0) { // 如果已经已还金额>=应还
				loanapplyExpand.setIsBackOver(StatusContexts.O2O_YES);
			}
			loanapplyExpandService.updateById(loanapplyExpand); // 修改expand表中的数据
			int i = o2OComBankFeeService.updateById(o2OComBackFee);
			if (i > 0) {
				log.info("ReferId:[{}],更新退费表状态成功", notifyParam.getReferId());
			} else {
				log.info("ReferId:[{}],更新退费表状态失败", notifyParam.getReferId());
			}
			// }
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {
			log.info("退费流程驳回：ReferId:[{}]", notifyParam.getReferId());
			O2OComBackFee o2OComBackFee = o2OComBankFeeService.queryEntity(notifyParam.getReferId(), StatusContexts.BZJ_STATUS_RUN);
			o2OComBackFee.setBillStatus(StatusContexts.BZJ_STATUS_NEW);
			int i = o2OComBankFeeService.updateById(o2OComBackFee);
			if (i > 0) {
				log.info("ReferId:[{}],驳回退保证金管理费成功", notifyParam.getReferId());
			} else {
				log.info("ReferId:[{}],驳回退保证金管理费失败", notifyParam.getReferId());
			}
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {
			log.info("退费流程作废：ReferId:[{}]", notifyParam.getReferId());

		}
		return loanapply;
	}
	
	/**
	 * 退还保证金、管理费和风险补偿金,插入明细表
	 */
	public LoanapplyExpand insertPaymentDetail(LoanapplyExpand loanapplyExpand, O2OComBackFee o2OComBackFee) {
		O2oPaymentDetailEntity o2oPaymentDetailEntity = new O2oPaymentDetailEntity();
		o2oPaymentDetailEntity.setBillCode(o2OComBackFee.getBillCode());
		o2oPaymentDetailEntity.setLoanapplyNo(o2OComBackFee.getLoanapplyNo());
		o2oPaymentDetailEntity.setPaymentType(StatusContexts.PAYMENT_TYPE_F);
		o2oPaymentDetailEntity.setHappenSource("贷款单号：" + o2OComBackFee.getBillCode());
		o2oPaymentDetailEntity.setOperationNo(o2OComBackFee.getCreatorId());
		o2oPaymentDetailEntity.setOperationName(o2OComBackFee.getCreatorName());
		if (o2OComBackFee.getPayDate() == null) {
			o2oPaymentDetailEntity.setUpdateTime(new Date());
		} else {
			o2oPaymentDetailEntity.setUpdateTime(o2OComBackFee.getPayDate());
		}
		o2oPaymentDetailEntity.setRecordTime(new Date());
		// 插入明细表
		if (o2OComBackFee.getBackDeposite().compareTo(BigDecimal.ZERO) > 0) {
			o2oPaymentDetailEntity.setSubjectType(StatusContexts.SUBJECT_TYPE_BZJ);
			o2oPaymentDetailEntity.setAmountMoney(o2OComBackFee.getBackDeposite());
			o2oPaymentDetailEntity.setRemark("系统单号:" + o2OComBackFee.getBillCode() + "退保证金：" + o2OComBackFee.getBackDeposite());
			o2oPaymentDetailService.saveEntity(o2oPaymentDetailEntity);
		}
		// 插入明细表
		if (o2OComBackFee.getBackManger().compareTo(BigDecimal.ZERO) > 0) {
			o2oPaymentDetailEntity.setId(null);
			o2oPaymentDetailEntity.setSubjectType(StatusContexts.SUBJECT_TYPE_GLF);
			o2oPaymentDetailEntity.setAmountMoney(o2OComBackFee.getBackManger());
			o2oPaymentDetailEntity.setRemark("系统单号:" + o2OComBackFee.getBillCode() + "退管理费：" + o2OComBackFee.getBackManger());
			o2oPaymentDetailService.saveEntity(o2oPaymentDetailEntity);
		}
		// 插入明细表
		if (o2OComBackFee.getBackRisk().compareTo(BigDecimal.ZERO) > 0) {
			o2oPaymentDetailEntity.setId(null);
			o2oPaymentDetailEntity.setSubjectType(StatusContexts.SUBJECT_TYPE_FXBCJ);
			o2oPaymentDetailEntity.setAmountMoney(o2OComBackFee.getBackRisk());
			o2oPaymentDetailEntity.setRemark("系统单号:" + o2OComBackFee.getBillCode() + "退风险补偿金：" + o2OComBackFee.getBackRisk());
			o2oPaymentDetailService.saveEntity(o2oPaymentDetailEntity);
		}
		if (loanapplyExpand.getRetaDeposite() != null) {
			loanapplyExpand.setRetaDeposite(loanapplyExpand.getRetaDeposite().add(o2OComBackFee.getBackDeposite()));

		} else {
			loanapplyExpand.setRetaDeposite(o2OComBackFee.getBackDeposite());
		}

		if (loanapplyExpand.getRetaManage() != null) {
			loanapplyExpand.setRetaManage(loanapplyExpand.getRetaManage().add(o2OComBackFee.getBackManger()));
		} else {
			loanapplyExpand.setRetaManage(o2OComBackFee.getBackManger());
		}
		if (loanapplyExpand.getRetaRisk() != null) {
			loanapplyExpand.setRetaRisk(loanapplyExpand.getRetaRisk().add(o2OComBackFee.getBackRisk()));

		} else {
			loanapplyExpand.setRetaRisk(o2OComBackFee.getBackRisk());
		}
		return loanapplyExpand;
	}
	
}
